package com.manager.controller;

import com.manager.bean.*;
import com.manager.service.AccountService;
import com.manager.service.ManagerService;
import com.manager.service.QRcodeService;
import com.manager.service.StudentAccessService;
import com.manager.util.MD5Utils;
import org.apache.coyote.OutputBuffer;
import org.apache.xmlbeans.impl.common.ReaderInputStream;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.lang.reflect.Array;
import java.util.Date;
import java.util.List;
import com.manager.util.tts;

/**
 * (Manager)表控制层
 *
 * @author makejava
 * @since 2021-03-02 20:15:30
 */
@Controller
public class ManagerController {
   /* *//**
     * 服务对象
     */
    @Resource
    private ManagerService managerService;

    @Resource
    private AccountService accountService;

    @RequestMapping(value = "/findManager")
    public String findManager(Model model,Integer Id, String Name, Integer pageIndex, Integer pageSize) {
        PageInfo<Manager> pi =  managerService.findPageInfo(Id,Name,pageIndex,pageSize);
        model.addAttribute("pi", pi);
        return "manager_list";
    }


    @RequestMapping(value = "/exportmanagerlist", method = RequestMethod.POST)
    @ResponseBody
    public List<Manager> exportManager() {
        List<Manager> managerList = managerService.getAll();
        return managerList;
    }


    @RequestMapping("/deleteManager")
    @ResponseBody
    public String deleteManager(Integer Id) {
        accountService.deleteById(Id);
        managerService.deleteById(Id);
        return "manager_list";
    }


    @RequestMapping(value = "/addManager", method = RequestMethod.POST)
    @ResponseBody
    public String addManager(@RequestBody Manager manager) {
        Account account = new Account();
        account.setAccount(manager.getId());
        account.setIdentity("housemaster");
        String salt = MD5Utils.getSalt();
        String password = MD5Utils.md5("123456"+salt);
        account.setSalt(salt);
        account.setPassword(password);
        accountService.insert(account);
        managerService.insert(manager);
        return "manager_list";
    }


    @RequestMapping(value = "/updateManager", method = RequestMethod.POST)
    public String updateManager(Manager manager) {
        managerService.update(manager);
        return "redirect:/findManager";
    }


    @RequestMapping("/findManagerById")
    public String findManagerById(Integer Id, HttpSession session, Model model) {
        Manager s = managerService.queryById(Id);
        session.setAttribute("s", s);
        model.addAttribute("s", s);
        return "manager_edit";
    }

    @Resource
    private QRcodeService qRcodeService;
    @Resource
    private StudentAccessService studentAccessService;

    @RequestMapping(value = "getCamera")
    public String getCamera(){
        return "QRcode";
    }

    @RequestMapping("/sendQRcode")
    @ResponseBody
    public void sendQRcode(String BrowserCode, HttpServletResponse response){
        int stuId = Integer.parseInt(BrowserCode.substring(0,10));
        QRcodeKey qRcodeKey = qRcodeService.queryById(stuId);
        String text = "二维码过期或者无效";
        //无效二维码
        if(qRcodeKey==null){
            text = "二维码过期或者无效";
        }
        //过期二维码
        else if(new Date().getTime()-qRcodeKey.getCreateTime()>5*60*1000){
            text = "二维码过期或者无效";
            qRcodeService.deleteById(stuId);
        }
        else if(qRcodeKey.getQrcode().equals(BrowserCode.substring(10))) {
            StudentAccess studentAccess = studentAccessService.queryByStuIdDESC(stuId);
            //出宿舍二维码
            if (studentAccess.getGetout() == null) {
                studentAccess.setGetout(new Date(new Date().getTime()));
                text = stuId + "同学，出宿舍";
            }
            //回宿舍二维码
            else if (studentAccess.getComeback() == null) {
                studentAccess.setComeback(new Date(new Date().getTime()));
                text = stuId + "同学，欢迎回到宿舍";
            }
            studentAccessService.update(studentAccess);
            qRcodeService.deleteById(stuId);

        }
        //返回合成语音
        try {
            new tts().getSound(text);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(new File("tts.wav"),"r");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int len = 0;
        byte[] bytes =new byte[1024];
        while (true){
            try {
                if (((len=raf.read(bytes))==-1)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                response.getOutputStream().write(bytes,0,len);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
